/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */

<template>
    <sf-form ref="form" class="add-index-form formWin" :label-width="100">
        <sf-form-item class="belong-search"
                      :default-hidden="searchEngineFlag">
            <sf-fieldlabel>
                <lang>所属搜索引擎</lang>
            </sf-fieldlabel>
            <span class="belong-index">{{searchEngineName}}</span>
        </sf-form-item>
        <sf-form-item>
            <sf-fieldlabel>
                <lang>索引名称</lang>
            </sf-fieldlabel>
            <sf-textfield
                v-model="submitData.value"
                :default-width="fieldWidth"
                :placeholder="_('请输入索引名称')"
                :allow-blank="false" />
        </sf-form-item>

        <sf-form-item>
            <sf-fieldlabel>
                <lang>索引别名</lang>
            </sf-fieldlabel>
            <sf-textfield
                v-model="submitData.alias"
                :placeholder="_('请输入索引别名')"
                :default-width="fieldWidth"
                :allow-blank="false" />
        </sf-form-item>

        <sf-form-item>
            <sf-fieldlabel>
                <lang>索引类型</lang>
            </sf-fieldlabel>
            <sf-textfield
                v-model="submitData.type"
                :default-width="fieldWidth"
                :allow-blank="false" />
        </sf-form-item>

        <sf-form-item>
            <sf-fieldlabel>
                <lang>索引存储周期</lang>
            </sf-fieldlabel>
            <sf-select v-model="submitData.storageCycle"
                       :default-width="fieldWidth"
                       :options="fieldTypeList"
                       :validator="_singleSelectorIsBlankValidator"
                       type="single" />
        </sf-form-item>
        <sf-form-item>
            <sf-fieldlabel>
                <lang>描述</lang>
            </sf-fieldlabel>
            <sf-textarea
                v-model="submitData.des"
                :default-width="fieldWidth"
                :max-length="300"
                :placeholder="_('输入对索引的描述，最多300字(选填）')"
                :allow-blank="true" />
        </sf-form-item>
    </sf-form>
</template>

<style lang="less">
    .add-index-form {
        .belong-search {
            display: flex;
            align-items: center;
            .belong-index {
                padding-left: 5px;
            }
        }
    }
</style>

<script>
const FIELD_WIDTH = 300;

const DEFAULT_VALUE = {
    alias: '',
    type: 'doc',
    des: '',
    storageCycle: '',
    value: ''
};

//按single、day、week、month、year存数据
const DATE_LIST = [
    {
        label: '无',
        value: 'single'
    },
    {
        label: '天',
        value: 'day'
    },
    {
        label: '周',
        value: 'week'
    },
    {
        label: '月',
        value: 'month'
    },
    {
        label: '年',
        value: 'year'
    }
];

export default {

    data () {
        return {
            submitData: Object.assign({}, DEFAULT_VALUE),
            connectionResult: '',
            connectionStatus: false,
            fieldTypeList: DATE_LIST,
            searchEngineName: '',
            searchEngineFlag: true
        };
    },


    computed: {
        fieldWidth () {
            return FIELD_WIDTH;
        }
    },

    methods: {
        _singleSelectorIsBlankValidator (v) {
            if (!v) {
                return _('该输入项不允许为空');
            }
            return true;
        },

        setJsonSearch (item) {
            this.searchEngineName = item;
            this.searchEngineFlag = false;
        },
        getJsonValue () {
            return this.submitData;
        },

        setJsonValue (data) {
            Object.assign(this.submitData, DEFAULT_VALUE, data);
        }
    }
};
</script>
